/**
 *Project: jh-frameworks
 *File: cn.xkshow.framework.web.action.UniversalAction.java 
 *Author: hardy(admin@xkshow.cn) Copyright
 ****************************************************************
 *   VERSION       DATE            BY       CHANGE/COMMENT
 * --------------------------------------------------------------
 *    1.0        2013-9-27         hardy         create
 ****************************************************************
 *note:
 ***************************************************************/ 
package cn.xkshow.framework.web.action;

import java.io.Serializable;
import java.util.Map;

import cn.xkshow.framework.service.UniversalService;

public abstract class UniversalAmAction<T> extends BaseMainAmAction {
	protected UniversalService<T> universalService;
	private Class<T> objClass;
	private Object objFrm; 

	protected UniversalAmAction(Class<T> oClass){		
		try {
			this.objClass = oClass;
			this.oModel = this.objClass.newInstance();
			this.objFrm = this.oModel;
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/*
	 * (non-Javadoc)
	 * Method.Abstract
	 */	
	public abstract String search();	
		
	/*
	 * (non-Javadoc)
	 * Method.Implemented
	 */
	public String input(){		
		return INPUT;
	}
	
	public String edit(){
		if(null!=super.kid && Integer.valueOf(kid)>0){
			this.oModel = this.viewByKey(null, Integer.valueOf(kid));
		}
		return input();
	}
	
	public String save(){	
		try {
			if(null!=super.kid && Integer.valueOf(kid)>0){
				this.universalService.saveOrUpdate(this.objFrm);
			}else {
				this.universalService.save(this.objFrm);
			}
			this.addActionMessage("SUCCESS!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			this.addActionError(this.getText("msg.action.fail"));
			return input();
		}
		
		return search();
	}
		
	public String esave(){
		try {
			if(null!=super.kid && Integer.valueOf(kid)>0){
				this.objFrm = this.universalService.getEntity(this.objClass, Integer.valueOf(kid));				
				copyPropertiesWithoutNull(this.objFrm, this.oModel);				
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return save();
	}
	
	public String delete(){
		try {
			this.universalService.deleteById(this.objClass, Integer.valueOf(kid));
			this.addActionMessage("SUCCESS!");
		} catch (Exception e) {
			// TODO: handle exception
			this.logger.error(e);
			this.addActionError(this.getText("msg.action.fail"));
		}	
		return search();
	}
	
	public String view(){
		if(null!=kid && Integer.valueOf(kid)>0){
			oModel = this.viewByKey(null, Integer.valueOf(kid));
			this.addActionMessage("SUCCESS: "+ oModel.toString());
		}
		return VIEW;
	}
	
	
	/*
	 * (non-Javadoc)
	 * Method.Implemented: Internal
	 */
	protected void findPageList(boolean isHQL, String querystring, Map<String, Object> paramMap, Integer viewLine, String pgurl){
		if(isHQL){
			super.setPageList(this.universalService.findPageListByHql(querystring, paramMap, super.getPage(viewLine, pgurl)));
		}else{
			super.setPageList(this.universalService.findPageListBySql(querystring, paramMap, super.getPage(viewLine, pgurl)));
		}
	}
	
	protected T viewByKey(String key, Object value){
		if(null != value){
			if(null == key){
				return this.universalService.getEntity(this.objClass, (Serializable)value);				
			}
			return this.universalService.getEntity(this.objClass, key, value);
		}
		return null;
	}
		
	/*
	 * (non-Javadoc)
	 * getter-setter
	 */	
	public void setUniversalService(UniversalService<T> universalService) {
		this.universalService = universalService;
	}
	
}
